package com.leiduoduo.oms.util;

import com.leiduoduo.oms.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageUtil {
    /**
     * 通过指定要运行的sevice，及其方法。并赋予参数，得到返回的数据与总数目
     * @param current 当前页数
     * @param pageSize 每页条数
     * @param serviceName service的名字
     * @param methodName 得到具体数据的方法的名字
     * @param totalMothedName 得到数据数量的方法的名字
     * @param map 查询条件
     * @return
     * @throws NoSuchMethodException 找不到方法
     * @throws ClassNotFoundException 找不到sevice
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */

    public Map<String, Object> page(int current, int pageSize, String serviceName, String methodName, String totalMothedName, Object dao, Map<String, Object> map) throws NoSuchMethodException, ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
        Map<String, Object> result = new HashMap<>();
        Class service = Class.forName(serviceName);
        Method method = service.getMethod(methodName, int.class, int.class, Object.class, Map.class);
        Method method2 = service.getMethod(totalMothedName,Object.class,Map.class);
        result.put("objectList",method.invoke(service.newInstance(),current, pageSize, dao, map));
        result.put("total",method2.invoke(service.newInstance(), dao, map));
        return result;
    }
}
